import {
  addShareApply,
  getPreviewLinkPath,
  queryMyFiles,
  queryOptRight,
  queryShareFiles,
  quickDownloadShareFiles,
  queryDatadictList,
  queryScanTree,
} from '@/services/workspace';
import { queryOrgAdminByUserCode,} from '@/services/system/org';

export default {
  namespace: 'shareModel',

  state: {
    // 列表数据
    data: [],
    queryForm: {},
    orgAdmin:[],
  },

  effects: {
    * queryShareFiles({payload}, {call, put}) {
      const response = yield call(queryShareFiles, payload);
      if (response && response.result == 'ok') {
        yield put({
          type: 'save',
          payload: response,
        });
      }else{
        yield put({
          type: 'save',
          payload: [],
        });
      }

    },

    * queryScanTree({payload, callback,}, {call, put}) {
      const response = yield call(queryScanTree, payload);

      if (response.result == 'ok') {
        yield put({
          type: 'saveScanData',
          payload: response,
        });
      }

    },


    * queryMyFiles({payload}, {call, put}) {
      const response = yield call(queryMyFiles, payload);
      if (response && response.result == 'ok') {
        yield put({
          type: 'save',
          payload: response,
        });
      }else{
        yield put({
          type: 'save',
          payload: [],
        });
      }

    },

    * queryHideFunctions({payload, callback,}, {call, put}) {
      const response = yield call(queryDatadictList, payload);
      if (callback) callback(response);
    },

    //申请下载
    * addShareApply({payload, callback}, {call, put}) {
      const response = yield call(addShareApply, payload);
      if (callback) callback(response);
    },
    * getPreviewLinkPath({payload, callback}, {call, put}) {
      const response = yield call(getPreviewLinkPath, payload);
      if (callback) callback(response);
    },
//下载
    * quickDownloadShareFiles({payload, callback}, {call, put}) {
      const response = yield call(quickDownloadShareFiles, payload);
      if (callback) callback(response);
    },


    * queryOptRight({payload, callback,}, {call, put}) {
      const response = yield call(queryOptRight, payload);
      if (callback) callback(response);
    },
    * queryOrgAdmin({payload, callback,}, {call, put}) {
      const response = yield call(queryOrgAdminByUserCode, payload);
      if(response&&response.result=='ok'){
        yield put({
          type: 'adminOrg',
          payload: response,
        });
      }

      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    /**
     * 文件扫描结果
     * @param state
     * @param action
     * @returns {{usersData: *}}
     */
    saveScanData(state, action) {
      return {
        ...state,
        scanData: action.payload.data,
      };
    },
    adminOrg(state, action) {
      return {
        ...state,
        orgAdmin: action.payload.data,
      };
    },
  },
};
